package com.amado.easyui2.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.amado.easyui2.constant.GlobalConstants;
import com.amado.easyui2.domain.CoachCarEntity;
import com.amado.easyui2.domain.Department;
import com.amado.easyui2.exception.ApiException;
import com.amado.easyui2.service.CoachService;
import com.amado.easyui2.util.JsonUtil;
import com.amado.easyui2.util.SessionUtils;
import com.amado.easyui2.vo.Page;
import com.amado.easyui2.vo.Result;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CoachServlet extends HttpServlet {
      CoachService coachService;
	private static ObjectMapper mapper = new ObjectMapper();
	private static final long serialVersionUID = 1L;
	ApplicationContext applicationContext;
	@Override
	public void init() throws ServletException {
		//Spring容器
		applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
				//通过容器获取Bean
		coachService = applicationContext.getBean("coachServiceImpl",CoachService.class);
	}

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		arg0.setCharacterEncoding("UTF-8");
		arg1.setCharacterEncoding("UTF-8");
		String method = arg0.getParameter("method");
		if ("add".equals(method)) {
			add(arg0, arg1);
		} else if ("delete".equals(method)) {
			delete(arg0, arg1);
		} else if ("edit".equals(method)) {
			edit(arg0, arg1);
		} else if ("update".equals(method)) {
			update(arg0, arg1);
		} else if ("query".equals(method)) {
			query(arg0, arg1);
		}
	}


	private void query(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		Page page = new Page();
		String strPage = arg0.getParameter("page");
		String strRows = arg0.getParameter("rows");
		int pageOne = strPage == null ? 1 : Integer.parseInt(strPage);
		int rows = strRows == null ? 5 : Integer.parseInt(strRows);
		String tm = arg0.getParameter("tm");
		String lcn = arg0.getParameter("lcn");
		page.setCurrentPage(pageOne);
		page.setRowSize(rows);
		Page<CoachCarEntity> pageList = coachService.listAll(tm, lcn, page);
//		将数据传入页面
		PrintWriter out = arg1.getWriter();
		out.print(mapper.writeValueAsString(pageList));
	}

	private void update(HttpServletRequest arg0, HttpServletResponse arg1) {
		String strJson = arg0.getParameter("data");
		CoachCarEntity coachInfo = JsonUtil.jsonToBean(strJson, CoachCarEntity.class);
		coachInfo.setCreateName(SessionUtils.getUserName(arg0));
		coachService.update(coachInfo);
	}

	private void edit(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		Long id = Long.valueOf(arg0.getParameter("id"));
		arg1.getWriter().print(mapper.writeValueAsString(coachService.getById(id)));
	}
//	Long id = Long.valueOf(arg0.getParameter("id"));
//	List<User> list = userService.list();
//	for (User user : list) {
//		if (user.getId().equals(id)) {
//			User userId = user;
//			PrintWriter out = arg1.getWriter();
//			out.print(mapper.writeValueAsString(userId));
//			break;

	private void delete(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
//		String delete = arg0.getParameter("delete");
//		if (delete != null && delete.trim() != "") {
//			coachService.delete(Long.valueOf(delete));
//			arg1.getWriter().print(JsonUtil.beanToJson(new Result()));
//		} else {
//			arg1.getWriter().print(JsonUtil.beanToJson(new Result("0", "参数错误")));
//		}
		PrintWriter out = arg1.getWriter();
		try {
			String ids = arg0.getParameter("ids");
			if (ids != null) {
				String[] idArray = ids.split(",");
				if (idArray.length > 5) {
					out.println(mapper.writeValueAsString(new Result("2", "删除的数不能超过5个")));
				} else {
					List<Long> idList = new ArrayList<>();
					for (String tempId : idArray) {
						idList.add(Long.valueOf(tempId));
					}
					coachService.delete(idList);
					out.print(mapper.writeValueAsString(new Result()));
				}
			}
		} catch (Exception e) {
			out.print(mapper.writeValueAsString(new Result("2", e.getMessage())));
		}
	}

	private void add(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		try {
		String strJson = arg0.getParameter("data");
		CoachCarEntity coachEntityInfo = JsonUtil.jsonToBean(strJson, CoachCarEntity.class);
		String createName = (String) arg0.getSession().getAttribute(GlobalConstants.SESSION_USER);
		coachEntityInfo.setCreateName(createName);
		coachService.add(coachEntityInfo);
		}catch (ApiException e) {
			arg1.getWriter().print(JsonUtil.beanToJson(new Result(e.getCode(),e.getMessage())));
		}
	}
}